package com.cskaoyan.day20;

import java.io.*;

public class ThreadCopy {
    public static void main(String[] args) {
        Thread t1 = new Thread1();
        t1.setName("线程1");
        Thread t2 = new Thread2();
        t2.setName("线程2");
        Thread t3 = new Thread3();
        t3.setName("线程3");
        t1.start();
        t2.start();
        t3.start();
    }


}

class Thread1 extends Thread {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {
                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(getName() + ":“文件已复制" + String.format("%.2f",res) + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + ":“文件已复制完成！");
    }
}

class Thread2 extends Thread {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a1.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {

                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(getName() + ":“文件已复制" + String.format("%.2f",res) + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + ":“文件已复制完成！");
    }
}

class Thread3 extends Thread {
    static public long time1 = System.currentTimeMillis();

    @Override
    public void run() {
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("a.txt"));
             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/a2.txt"))) {
            int d;
            double count = 0;
            while ((d = in.read()) != -1) {
                out.write(d);
                count++;
                if (count % 200 == 0) {

                    double res = ((count * 100) / (new File("a.txt").length()));
                    System.out.println(getName() + ":“文件已复制" + String.format("%.2f",res) + "%");
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + ":“文件已复制完成！");
    }
}